Ext.define('AM.view.DetailPostView', {
		extend: 'Ext.grid.Panel',
		alias : 'widget.detailPostView',
		store : 'DetailPostStore',
		columnLines:true,
//		selModel : Ext.create('Ext.selection.CheckboxModel'), 
		selModel: {
	        injectCheckbox: 0,
	        mode: "SIMPLE"    //"SINGLE"/"SIMPLE"/"MULTI"
	       
   		 },
   		selType: "checkboxmodel",
    	dockedItems: [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store : 'DetailPostStore',
        displayInfo: true
    }],
		columns: [	
			        {text: '序号', dataIndex: 'id',width:50,align:'center'},
			        { text: '岗位人员', dataIndex: 'employee', width:100,align:'center'},
			        { text: '科室', dataIndex: 'department', width:100,align:'center'},
			        { text: '名称', dataIndex: 'name', width:200,align:'center'},
			        { text: '时长', dataIndex: 'postHour', width:80,align:'center'},
			        { text: '岗位系数', dataIndex: 'Coffe', width:80,align:'center'},
			        { text: '内容', dataIndex: 'adcontent', width:500,align:'center'}
				 ],
			   tbar: [
					  { xtype: 'button',id:'tab-7_add', text: '增加',handler: addPost,icon: 'Ext/restful/images/add.png'
					  },
					  { xtype: 'button', id:'tab-7_edit', text: '修改',handler: editPost,icon: 'Ext/restful/images/cog_edit.png'
					  },
					  { xtype: 'button', id:'tab-7_delete',text: '删除',handler: deletePost,icon: 'Ext/restful/images/delete.png'
					  },
					   {
					     xtype : "combobox",
					     allowBlank:false,
					     labelAlign :"right",
						 fieldLabel: '选择科室',
						 emptyText:'请选择科室',
					     queryMode: 'local',
						 store :Ext.create('AM.store.TargetDepartStore'),
						 displayField: 'name',
						 valueField: 'id',
					     width:300,
					     listeners:{   
										select:function(combo, record,index){
											 try{
												 var parent=Ext.getCmp('search_emp');
												 parent.clearValue();
												 parent.store.load({params:{param1:this.value}});
												
											 }
											 catch(ex){
												 Ext.MessageBox.alert("错误","数据加载失败。");
											 }
										}
									}
					  },{
					  	 xtype : "combobox",
					     allowBlank:false,
					     labelAlign :"right",
						 fieldLabel: '选择人员',
						 id:'search_emp',
						 emptyText:'请选人员',
						 queryMode: 'local',
						 store :Ext.create('AM.store.SearchEmpStore'),
						 displayField: 'name',
						 valueField: 'id',
					     width:300
					  },
					  { xtype: 'button', text: '查询', id:'tab-7_search',handler: searchdetaipost,icon : 'Ext/restful/images/find.png'
					  }
					]
})
function searchdetaipost(){
	var grid=this.ownerCt.ownerCt;
			var emp= Ext.getCmp('search_emp').getValue();
		    grid.getStore().on('beforeload', function (store, options) {
	          var new_params = {serchemp:emp}
	                Ext.apply(store.proxy.extraParams, new_params);
      		 });//此处为加载条件
		 grid.getStore().reload();
					
}
 function addPost() {
		 var myform1= new Ext.form.Panel({
			        labelWidth : 65,
			        bodyPadding: 15,
				    items: [{
					   		 layout : "column",
							 border : false,
							 items:[{
								 	     columnWidth :.35,
								         layout : "form",
								         border : false,
					   			         items: [{
								   				     xtype : "numberfield",
													 nanText:"请输入0到176之间的数字",
												     labelAlign :"right",
												     fieldLabel: '岗位时长',
												     name: 'postHour',
												     blankText:"必须为数字",
												     minValue: 0,
												     maxValue:176 ,
												     minText:"最小值为0",
												     maxText:"最大值为176"
							        	}]	
								 }]
					   },{
					   	   layout : "column",
						   border : false,
						   items:[{
								      columnWidth :.45,
								      layout : "form",
								      border : false,
					   			      items: [{
								   		        xtype : "combobox",
											     allowBlank:false,
											     labelAlign :"right",
												 fieldLabel: '所属科室',
												 emptyText:'请选择科室',
												 name: 'department.id',
												 queryMode: 'local',
												 store :"TargetDepartStore",
												 displayField: 'name',
				    							 valueField: 'id',
				    							 listeners:{   
																select:function(combo, record,index){
																	 try{
																		 var parent=Ext.getCmp('post11');
																		 var parent1=Ext.getCmp('employee_11');
																		 parent.clearValue();
																		 parent1.clearValue();
																		 parent.store.load({params:{param:this.value}});
																		 parent1.store.load({params:{param1:this.value}});
																	 }
																	 catch(ex){
																		 Ext.MessageBox.alert("错误","数据加载失败。");
																	 }
																}
															}
							        	}]	
								 	
								 },{
								 	   columnWidth :.45,
									    layout : "form",
									    border : false,
						   			    items: [{
									   		        xtype : "combobox",
									   			    allowBlank:false,
									   			    fieldLabel: '人员',
									   			    emptyText:'请选择人员',
									   			    id:'employee_11',
									   			    labelAlign :"right",
									   			    store :"SearchEmpStore",
													queryMode: 'local',
											        name: 'employee.id',
											        displayField: 'name',
					    							valueField: 'id'
								        	}]
								 	
								 }]
					   	
					   },{
					   	   layout : "column",
						   border : false,
							   items:[{
							   			 columnWidth :.45,
									      layout : "form",
									      border : false,
						   			      items: [{
									   		         xtype : "combobox",
												     allowBlank:false,
												     labelAlign :"right",
													 fieldLabel: '所属岗位',
													 emptyText:'请选择岗位',
													 name: 'post.id',
													 id:'post11',
													 store :"PostStore",
													 queryMode: 'local',
													 displayField: 'name',
					    							 valueField: 'id',
					    							  listeners:{   
															select:function(combo, record,index){
																 try{
																	 var parent=Ext.getCmp('SonPost11');
																	 parent.clearValue();
																	 parent.store.load({params:{param:this.value}});
																 }
																 catch(ex){
																	 Ext.MessageBox.alert("错误","数据加载失败。");
																 }
															}
													}
								        	}]	
							   },{
							   		 columnWidth :.45,
								      layout : "form",
								      border : false,
					   			      items: [{
								   		         xtype : "combobox",
											     allowBlank:false,
											     labelAlign :"right",
												 fieldLabel: '所属子岗',
												 emptyText:'请选择岗位',
												 name: 'sonPost.id',
												 id:'SonPost11',
												 store :"searchSonPostStore",
												 queryMode: 'local',
												 displayField: 'name',
				    							 valueField: 'id'
							        	}]
							   	
							   }]
					   }]		        	
				    
		 })
		    var grid=this.ownerCt.ownerCt;
			var win = new Ext.Window({
				title : "添加员工岗位",
				width : 700,
				height : 300,
				frame: true,
				layout:'fit',
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
	
								win.getComponent(0).getForm().submit({
											 url : 'detailPostAction_addDetailPost',
											 success : function() {
												Ext.Msg.alert('提示', '添加成功');
												win.close();
												grid.getStore().reload();
											},
											failure : function() {
												Ext.Msg.alert('错误', '添加失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
								win.close();
											
							}
						}]
			});
		win.show();
	}

   function editPost() {
   		 var grid=this.ownerCt.ownerCt;
		 var myform1= new Ext.form.Panel({
			 
			        bodyPadding: 15,
			        labelWidth : 65,
			        items: [{
					   		 layout : "column",
							 border : false,
							 items:[{
								 	     columnWidth :.35,
								         layout : "form",
								         border : false,
					   			         items: [{
								   				     xtype : "numberfield",
													 nanText:"请输入0到176之间的数字",
												     labelAlign :"right",
												     fieldLabel: '岗位时长',
												     name: 'postHour',
												     blankText:"必须为数字",
												     minValue: 0,
												     maxValue:176 ,
												     minText:"最小值为0",
												     maxText:"最大值为176"
							        	}]	
								 }]
					   },{
					   	   layout : "column",
						   border : false,
						   items:[{
								      columnWidth :.45,
								      layout : "form",
								      border : false,
					   			      items: [{
								   		        xtype : "combobox",
											     allowBlank:false,
											     labelAlign :"right",
												 fieldLabel: '所属科室',
												 emptyText:'请选择科室',
												 name: 'department.id',
												 queryMode: 'local',
												 store :"TargetDepartStore",
												 displayField: 'name',
				    							 valueField: 'id',
				    							 listeners:{   
																select:function(combo, record,index){
																	 try{
																		 var parent=Ext.getCmp('post11');
																		 var parent1=Ext.getCmp('employee_11');
																		 parent.clearValue();
																		 parent1.clearValue();
																		 parent.store.load({params:{param:this.value}});
																		 parent1.store.load({params:{param1:this.value}});
																		 
																	 }
																	 catch(ex){
																		 Ext.MessageBox.alert("错误","数据加载失败。");
																	 }
																}
															}
							        	}]	
								 	
								 },{
									 	columnWidth :.45,
									    layout : "form",
									    border : false,
						   			    items: [{
									   		        xtype : "combobox",
									   			    allowBlank:false,
									   			    fieldLabel: '人员',
									   			    emptyText:'请选择人员',
									   			    id:'employee_11',
									   			    labelAlign :"right",
									   			    store :"SearchEmpStore",
													queryMode: 'local',
											        name: 'employee.id',
											        displayField: 'name',
					    							valueField: 'id'
//					    							
								        	}]
								 }]
					   	
					   },{
					   	   layout : "column",
						   border : false,
						   items:[{
						   			  columnWidth :.45,
								      layout : "form",
								      border : false,
					   			      items: [{
								   		         xtype : "combobox",
											     allowBlank:false,
											     labelAlign :"right",
												 fieldLabel: '所属岗位',
												 name: 'post.id',
												 emptyText:'请选择岗位',
												 id:'post11',
												 store :"PostStore",
												 queryMode: 'local',
												 displayField: 'name',
				    							 valueField: 'id',
				    							  listeners:{   
															select:function(combo, record,index){
																 try{
																	 var parent=Ext.getCmp('SonPost11');
																	 parent.clearValue();
																	 parent.store.load({params:{param:this.value}});
																 }
																 catch(ex){
																	 Ext.MessageBox.alert("错误","数据加载失败。");
																 }
															}
													}
							        	}]	
						        },{
						             columnWidth :.45,
								      layout : "form",
								      border : false,
					   			      items: [{
								   		         xtype : "combobox",
											     allowBlank:false,
											     labelAlign :"right",
												 fieldLabel: '所属子岗',
												 emptyText:'请选择岗位',
												 name: 'sonPost.id',
												 id:'SonPost11',
												 store :"searchSonPostStore",
												 queryMode: 'local',
												 displayField: 'name',
				    							 valueField: 'id'
							        	}]
						        }]
					   },{
					   	   layout : "column",
						   border : false,
						   items:[{
						   		    columnWidth :.3,
								    layout : "form",
								    border : false,
					   			    items: [{
								   		        xtype : "textfield",
								   			    allowBlank:false,
								   			    fieldLabel: 'ID',
								   			    labelAlign :"right",
										        name: 'id',
											    hidden:true
							        	}]
						   }]
					   	
					  }]		        		        	
		
		 })
		  var win = new Ext.Window({
				title : "修改员工岗位",
				width : 700,
				height : 300,
				layout:'fit',
				frame:true,
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'detailPostAction_editDetailPost',
											 success : function() {
												Ext.Msg.alert('提示', '修改成功');
												win.close();
												grid.getStore().reload();
												grid.getSelectionModel().deselectAll();
											},
											failure : function() {
												Ext.Msg.alert('错误', '修改失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
									win.close();
									grid.getSelectionModel().deselectAll();
											
							}
						}]
			});
//		var grid=this.ownerCt.ownerCt;
		var selections = grid.getSelectionModel().getSelection();
						if(selections.length==0){
							Ext.MessageBox.alert("提示","请选择要编辑的一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							var record = selections[0];
						//这里是修改操作的实现部分
							win.getComponent(0).getForm().loadRecord(record);
							win.show();
						}
	}

	function deletePost(){
				 var grid=this.ownerCt.ownerCt;
			     var selections = grid.getSelectionModel().getSelection();
					if(selections){
					if(selections.length==0){
							Ext.MessageBox.alert("提示","请检查是否选中某一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							Ext.MessageBox.confirm("提示", "确认要删除 ？", function(btn) {
							if (btn == 'yes') {			
							var record = selections[0];
								Ext.Ajax.request({
										url : 'detailPostAction_deleteDetailPost',
										success : function() {
											Ext.MessageBox.alert("提示",
													"删除" + record.get("name")+ "成功！");
												grid.remove(selections );
												grid.getStore().reload();
										},
										failure : function() {
											Ext.MessageBox.alert("提示", "岗位呗引用，删除失败！");
										},
										params : {
											id : record.get("id")
										}
									});			
								}
					  	})
                   	} 
 			   
		   }				
	}
	 	
